package weifei.wx.community.controller;


import com.auth0.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import weifei.web.Result;
import weifei.wx.community.entity.dto.TieCommentNoticeDTO;
import weifei.wx.community.mapper.TieCommentNoticeMapper;
import weifei.wx.community.service.TieCommentService;
import weifei.wx.community.service.TieService;


@RestController
@RequestMapping("/tie/comment/notice")
public class TieCommentNoticeController
{

    public static final int PAGE_COUNT = 5;

    @Autowired
    private TieService tieService;
    @Autowired
    private TieCommentService tieCommentService;

    @Autowired
    private TieCommentNoticeMapper tieCommentNoticeMapper;


    @RequestMapping("/getNoticeByPage")
    public Result getNoticeByPage(HttpServletRequest request, @Valid @NotNull Integer page)
    {
        String token = request.getHeader("access_token");
        var uid = JWT.decode(token).getClaim("user_id").asLong();

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .where("tie_user_id = ?", uid)
                .orderBy("tie_comment_id desc")
                .limit(PAGE_COUNT * (page - 1), PAGE_COUNT);
        var list = this.tieCommentNoticeMapper.selectListByQuery(queryWrapper);
        var dtoArray = new TieCommentNoticeDTO[list.size()];

        for (int i = 0; i < list.size(); i++)
        {
            final var notice = list.get(i);
            final var commentDTO = this.tieCommentService.getTieCommentByID(notice.getTieCommentId());
            final var tieDTO = this.tieService.getTieByID(commentDTO.getTieId());
            dtoArray[i] = new TieCommentNoticeDTO(notice, tieDTO, commentDTO);
        }
        return Result.success("success", dtoArray);
    }
}



